module "open-service" {
  source = "./modules/0-open-service"
}

data "alicloud_zones" "all" {
}

locals {
  private_key_file = file("${path.module}/config/key/id_dsa")
  public_key_file  = file("${path.module}/config/key/public.pem")
}

module "ram" {
  source = "./modules/1-ram"

  name = var.ram_name
}

output "ak" {
  value = format("id = %s, key = %s", module.ram.ak.AccessKeyId, module.ram.ak.AccessKeySecret)
}

module "vpc" {
  source = "./modules/2-vpc"

  vpc_name        = var.vpc_name
  vpc_cidr        = var.vpc_cidr
  vpc_description = var.vpc_description
  vpc_tags = {
    created = "terraform"
  }

  vswitch_name       = var.vswitch_name
  availability_zones = data.alicloud_zones.all.ids
  vswitch_cidrs      = [for i, v in data.alicloud_zones.all.ids : format("192.168.%d.0/24", i + 1)]
  vswitch_tags = {
    created = "terraform"
  }

  enabled_nat_gateway = var.enabled_nat_gateway
  nat_gateway_name    = var.nat_gateway_name
  nat_gateway_tags = {
    created = "terraform"
  }

}

module "security" {
  source = "./modules/3-security"

  name            = var.security_group_name
  key_pair_name   = var.key_pair_name
  vpc_id          = module.vpc.vpc_id
  public_key_file = local.public_key_file
}

module "rds-redis" {
  source = "./modules/4-rds-redis"

  exist_azs         = module.vpc.availability_zones
  exist_vswitch_ids = module.vpc.vswitch_ids

  instance_name         = var.rds_instance_name
  instance_type         = var.rds_instance_type
  instance_storage_type = var.rds_instance_storage_type
  instance_storage      = var.rds_instance_storage
  databases             = var.rds_databases
  accounts              = var.rds_accounts
}

module "ecs" {
  source = "./modules/5-ecs"

  number_of_instances = var.ecs_number

  exist_azs         = module.vpc.availability_zones
  exist_vswitch_ids = module.vpc.vswitch_ids
  security_groups   = module.security.security_group_ids
  key_name          = module.security.key_name

  name                        = var.ecs_name
  host_name                   = var.ecs_host_name
  memory_size                 = var.ecs_memory_size
  cpu_core_count              = var.ecs_cpu
  instance_type_family        = var.ecs_instance_type_family
  internet_max_bandwidth_out  = var.internet_max_bandwidth_out
  use_num_suffix              = true
  associate_public_ip_address = true

  // setup sql
  private_key           = local.private_key_file
  content               = file("${path.module}/config/sql/pipeline.sql")
  triggers              = module.rds-redis.alicloud_db_account_privilege
  rds_connection_string = module.rds-redis.connection_string
  rds_username          = lookup(element(var.rds_accounts, 0), "name")
  rds_passpwrd          = lookup(element(var.rds_accounts, 0), "password")
}

module "slb" {
  source = "./modules/6-slb"

  exist_azs            = module.vpc.availability_zones
  exist_vswitch_ids    = module.vpc.vswitch_ids
  instance_ids         = module.ecs.instance_id
  virtual_server_group = var.virtual_server_group
}

module "appid" {
  source = "./modules/9-appid"

  tenant_name     = var.tenant_name
  accessKeyId     = module.ram.ak.AccessKeyId
  accessKeySecret = module.ram.ak.AccessKeySecret
}

output "foo_appid" {
  value = [module.appid.app_id, module.appid.xj_app_id]
}

//module "config" {
//  source   = "./modules/10-config"
//  job_name = "terraform"
//}

module "ci" {
  source   = "./modules/11-ci"
  jobs = [
    {
      name: "ebike"
      template: templatefile("${path.module}/config/tpl/pipeline.xml.tpl", {
        description = "An example pipeline job as Terraform"
      })
    },
    {
      name: "worker"
      template: templatefile("${path.module}/config/tpl/pipeline.xml.tpl", {
        description = "An example pipeline job as Terraform"
      })
    },
    {
      name: "mbserver"
      template: templatefile("${path.module}/config/tpl/pipeline.xml.tpl", {
        description = "An example pipeline job as Terraform"
      })
    },
    {
      name: "device"
      template: templatefile("${path.module}/config/tpl/pipeline.xml.tpl", {
        description = "An example pipeline job as Terraform"
      })
    },
  ]
}